﻿using FastCodeGeneration.Entity;
using FastCodeGeneration.IService;
using FastCodeGeneration.Service;
using Microsoft.AspNetCore.Mvc;
using FastCodeGeneration.Models;
using FastCodeGeneration.Core.Framework;

namespace FastCodeGeneration.Controllers
{
    public class TableController : Controller
    {
        private readonly ITableService _tableService;

        private readonly ITableColumnService _tableColumnService;

        private readonly ICodeGeneratorService _codeGeneratorService;

        private readonly IProjectService _projectService;

        public TableController(ITableService tableService, ITableColumnService tableColumnService, ICodeGeneratorService codeGeneratorService, IProjectService projectService)
        {
            _tableService = tableService;
            _tableColumnService = tableColumnService;
            _codeGeneratorService = codeGeneratorService;
            _projectService = projectService;
        }

        public IActionResult Index()
        {
            var projectList = _projectService.SelectAll();
            return View(projectList);
        }
        public IPage<Table> GetCondition(Condition condition)
        {
            var page = _tableService.SelectByCondition(condition);
            return page;
        }

        public IActionResult FieldConfig(int id)
        {
            var entity = _tableService.SelectById(id);
            ViewBag.TableName = entity.Name;
            ViewBag.ProjectId = entity.ProjectId;
            return View();
        }


        public AjaxResult SaveConfig(List<TableColumn> columns)
        {
            _tableColumnService.BatchUpdate(columns);
            return new AjaxResult { Code = 200, Message = "保存成功" };
        }

        public AjaxResult Code(int projectId, List<string> ids)
        {
            _codeGeneratorService.CodeGeneration(projectId, ids);
            return new AjaxResult { Code = 200, Message = "生成成功" };
        }

        public AjaxResult DeleteCode(int projectId, List<string> ids)
        {
            _codeGeneratorService.DeleteCode(projectId, ids);
            return new AjaxResult { Code = 200, Message = "删除成功" };
        }

        public List<TableColumn> GetColumnList(int projectId, string tableName)
        {
            return _tableColumnService.GetTableColumns(projectId, tableName);
        }
    }
}
